class <%= class_name %>AssetsController < ApplicationController

  before_filter :login_required, :only => [:new, :edit, :update, :create]
  before_filter :check_administrator_role, :only => [:destroy]
    
  # GET /<%= file_name %>_assets
  # GET /<%= file_name %>_assets.xml
  def index
    @<%= file_name %>_assets = <%= class_name %>Asset.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @<%= file_name %>_assets }
    end
  end

  # GET /<%= file_name %>_assets/1
  # GET /<%= file_name %>_assets/1.xml
  def show
    @<%= file_name %>_asset = <%= class_name %>Asset.find_by_file(params[:id])
    respond_to do |format|
      if @<%= file_name %>_asset.instance_of?(<%= class_name %>Image)
        format.html {render :template => "<%= file_name %>_assets/image_show.html.erb"}
      else
        format.html # show.html.erb
      end
      format.xml  { render :xml => @<%= file_name %>_asset }
    end
  end

  # GET /<%= file_name %>_assets/new
  # GET /<%= file_name %>_assets/new.xml
  def new
    @<%= file_name %>_asset = <%= class_name %>Asset.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @<%= file_name %>_asset }
    end
  end

  # GET /<%= file_name %>_assets/1/edit
  def edit
    @<%= file_name %>_asset = <%= class_name %>Asset.find_by_file(params[:id])
  end

  # POST /<%= file_name %>_assets
  # POST /<%= file_name %>_assets.xml
  def create
    
    # if the asset is an image, use the WikiImage class
    if params[:<%= file_name %>_asset][:file].content_type.index("image")
      @<%= file_name %>_asset = <%= class_name %>Image.new(params[:<%= file_name %>_asset])
    else
      @<%= file_name %>_asset = <%= class_name %>Asset.new(params[:<%= file_name %>_asset])
    end
    
    @<%= file_name %>_asset.user_id = current_user.id
    
    respond_to do |format|
      if @<%= file_name %>_asset.save

        format.js {flash[:success] = "Your file has been uploaded. You can now add '#{@<%= file_name %>_asset.file.filename}' to a wiki page!"
                   @<%= file_name %>_asset = nil #clear this so the form sees it as a new upload
                   responds_to_parent do
                     render :update do |page|
                       page.replace_html 'new_<%= file_name %>_asset', :partial => "remote_form.html.erb"
                     end
                   end}
        
        #format.js   { render :partial => "remote_form.html.erb" }
        format.html { redirect_to(asset_path(@<%= file_name %>_asset.file.filename)) }
        format.xml  { render :xml => @<%= file_name %>_asset, :status => :created, :location => @file_asset }
      else
        format.js {responds_to_parent do
                     render :update do |page|
                       page.replace_html 'new_<%= file_name %>_asset', :partial => "remote_form.html.erb"
                     end
                   end}
        #format.js   { render :partial => "remote_form.html.erb" }
        format.html { render :action => "new" }
        format.xml  { render :xml => @<%= file_name %>_asset.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /<%= file_name %>_assets/1
  # PUT /<%= file_name %>_assets/1.xml
  def update
    @<%= file_name %>_asset = <%= class_name %>Asset.find_by_file(params[:id])

    respond_to do |format|
      if @<%= file_name %>_asset.update_attributes(params[:<%= file_name %>_asset])
        flash[:notice] = '<%= class_name %>Asset was successfully updated.'
        format.html { redirect_to(asset_path(@<%= file_name %>_asset.file.filename)) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @<%= file_name %>_asset.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /<%= file_name %>_assets/1
  # DELETE /<%= file_name %>_assets/1.xml
  def destroy
    @<%= file_name %>_asset = <%= class_name %>Asset.find_by_file(params[:id])
    @<%= file_name %>_asset.destroy

    respond_to do |format|
      format.html { redirect_to(assets_url) }
      format.xml  { head :ok }
    end
  end
end
